#################################################################################
### Racialized Misinformation, Factual Corrections, and Prejudicial Attitudes ###
### [Minimum Detectable Effects Analysis for Study 1]                         ###
### Authors: Eddy S. F. Yeung, Joseph Glasgow                                 ###
### Date: June 26, 2025                                                       ###
#################################################################################

### Set-up ----
## Clean the working environment and set the working directory
rm(list = ls())
setwd("~/Desktop/racialized_misinfo/replication/Study 1") # set your working directory here, which should also contain the survey data ("study1_dataset.csv")

## Import the dataset
df <- read.csv("study1_dataset.csv")

### Code outcome variables ----
## Negative stereotype of Blacks in work ethics (0 = weakest; 6 = strongest)
df <- df %>% mutate(stereo = case_when(
  group == 0 ~ stereo_C,
  group == 1 ~ stereo_T
))

## Racial resentment (mean effects index with imputation)
df$rr_1 <- ifelse(df$group == 0, df$rr_C_1, df$rr_T_1)
df$rr_2 <- ifelse(df$group == 0, df$rr_C_2, df$rr_T_2)
df$rr_3 <- ifelse(df$group == 0, df$rr_C_3, df$rr_T_3)
df$rr_4 <- ifelse(df$group == 0, df$rr_C_4, df$rr_T_4)
df$rr_5 <- ifelse(df$group == 0, df$rr_C_5, df$rr_T_5)
rr_matrix <- cbind(df$rr_1, df$rr_2, df$rr_3, df$rr_4, df$rr_5)
cal_MEI <- 
  function(Z, outcome_mat, to_reorient, reorient = F, greedy = T, impute = F) {
    if(impute == T) {
      R <- 1 * is.na(outcome_mat)
      means_for_imputation <- 
        rbind(apply(outcome_mat[Z == 0, ], MAR = 2, FUN = mean, na.rm = T),
              apply(outcome_mat[Z == 1, ], MAR = 2, FUN = mean, na.rm = T))
      to_impute <- R * means_for_imputation[Z + 1, ]
      outcome_mat[is.na(outcome_mat)] <- 0
      outcome_mat <- outcome_mat + to_impute
    }
    c_mean <- apply(X = outcome_mat[Z == 0, ], MARGIN = 2, FUN = mean, na.rm = T)
    c_sd <- apply(X = outcome_mat[Z == 0, ], MARGIN = 2, FUN = sd, na.rm = T)
    z_score <- t(t(sweep(outcome_mat, 2, c_mean)) / c_sd)
    index_numerator <- rowSums(z_score)
    if(greedy == T) {
      n_outcomes <- rowSums(!is.na(z_score))
    }
    else if(greedy == F){
      n_outcomes <- ncol(outcome_mat)
    }
    index <- index_numerator / n_outcomes
    index <- (index - mean(index[Z == 0], na.rm = T)) / sd(index[Z == 0], na.rm = T)
    return(index)
  }
df <- df %>% 
  mutate(resent = cal_MEI(Z = df$group, outcome_mat = rr_matrix, impute = T))

### Conduct minimum detectable effects analysis on work ethic stereotype (Table S4) ----
## Calculate the SD of the variable in the control group
sd.ctrl <- sd(df$stereo[df$group == 0], na.rm = T)
sd.ctrl

## Calculate the SD of the variable in the treatment group
sd.treat <- sd(df$stereo[df$group == 1], na.rm = T)
sd.treat

## Calculate the sample size in the control group
n.ctrl <- length(df$stereo[df$group == 0][!is.na(df$stereo[df$group == 0])])
n.ctrl

## Calculate the sample size in the treatment group
n.treat <- length(df$stereo[df$group == 1][!is.na(df$stereo[df$group == 1])])
n.treat

## MDE calculation
# The true effect size must be at least 2.8 standard errors from zero to detect 
# it with 80% probability using 95% confidence intervals (Gelman and Hill 2006).
# To estimate the standard error of the ATE, we use equation 3.6 in Gerber and 
# Green (2012). Thus, we simply multiply 2.8 the standard error of the ATE to 
# calculate the MDE.
MDE <- 2.8 * sqrt((sd.ctrl^2/(n.ctrl/2)) + (sd.treat^2/(n.treat/2)))
MDE / sd(df$stereo, na.rm = T) # MDE in Cohen's d

### Conduct minimum detectable effects analysis on racial resentment (Table S4) ----
## Calculate the SD of the variable in the control group
sd.ctrl <- sd(df$resent[df$group == 0], na.rm = T)
sd.ctrl

## Calculate the SD of the variable in the treatment group
sd.treat <- sd(df$resent[df$group == 1], na.rm = T)
sd.treat

## Calculate the sample size in the control group
n.ctrl <- length(df$resent[df$group == 0][!is.na(df$resent[df$group == 0])])
n.ctrl

## Calculate the sample size in the treatment group
n.treat <- length(df$resent[df$group == 1][!is.na(df$resent[df$group == 1])])
n.treat

## MDE calculation
# The true effect size must be at least 2.8 standard errors from zero to detect 
# it with 80% probability using 95% confidence intervals (Gelman and Hill 2006).
# To estimate the standard error of the ATE, we use equation 3.6 in Gerber and 
# Green (2012). Thus, we simply multiply 2.8 the standard error of the ATE to 
# calculate the MDE.
MDE <- 2.8 * sqrt((sd.ctrl^2/(n.ctrl/2)) + (sd.treat^2/(n.treat/2)))
MDE / sd(df$resent, na.rm = T) # MDE in Cohen's d
